package com.example.a517.myapplication;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private FragmentAdapter fr;
    private Toolbar tl;

    private TabLayout.Tab one;
    private TabLayout.Tab two;
    private TabLayout.Tab three;
    private TabLayout.Tab four;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tl = findViewById(R.id.tl);
        setSupportActionBar(tl);
        initViews();

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, final float v, int i1) {
                Bitmap bitmap = null;
                if (i==0){
                    bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.one0);
                }else if (i==1){
                    bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.two2);
                }else if (i==2){
                    bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.three3);
                }
                Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
                    @Override
                    public void onGenerated(@Nullable Palette palette) {
                        Palette.Swatch viabrant=palette.getVibrantSwatch();
                        if (viabrant==null){
                            for (Palette.Swatch swatch:palette.getSwatches()){
                                viabrant=swatch;
                                break;
                            }
                        }
                        int rgb =viabrant.getRgb();

                        mTabLayout.setBackgroundColor(rgb);
                        tl.setBackgroundColor(rgb);
                        if (Build.VERSION.SDK_INT>21){
                            Window window=getWindow();
                            window.setStatusBarColor(rgb);
                        }
                    }
                });
            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });



    }

    private void initViews(){
        mViewPager=(ViewPager)findViewById(R.id.vp);
        fr=new FragmentAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(fr);

        mTabLayout=(TabLayout)findViewById(R.id.tb);
        mTabLayout.setupWithViewPager(mViewPager);

        one=mTabLayout.getTabAt(0);
        one=mTabLayout.getTabAt(1);
        one=mTabLayout.getTabAt(2);



    }
}
